const _ = require('lodash');
const async = require('async');
const services = require('../../services');

const commonService = services.common;
const eLancoService = services.eLanco;
const customService = services.custom;

module.exports = (req, res, callback) => {
  const { SysRole: roleModel, SysDictionary: dictionaryModel, SysDepartment: departmentModel } = commonService.getModels();
  const id = req.params.id;
  const formId = req.query.formId;
  const taskList = req.query.taskList;
  const customTasks = {};
  const tasks = {
    roleList: async.apply(commonService.getList, roleModel),
    dictionaryList: async.apply(commonService.getList, dictionaryModel, { order: 'name ASC' }),
    departmentList: async.apply(commonService.getList, departmentModel),
    approvalList: async.apply(eLancoService.getFormDetailApprovalList, formId),
    formDetail: async.apply(eLancoService.getFormDetailIncludeAll, formId, id),
  };
  if (!_.isEmpty(taskList)) {
    _.forEach(taskList, (taskItem) => {
      const customTaskService = customService.task[taskItem.taskName];
      if (_.isFunction(customTaskService)) customTasks[taskItem.keyName] = async.apply(customTaskService, formId, id);
    });
  }
  async.parallel(_.assign(tasks, customTasks), callback);
};
